package com.winit.openapi.intelligent.controller.builder;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.oms.spi.intelligent.recommend.vo.SourceInfoVo;
import com.winit.oms.spi.intelligent.recommend.vo.SourceMerchandiseVo;
import com.winit.oms.spi.outbound.vo.OutboundOrderEnumContants;
import com.winit.oms.spi.outbound.vo.OutboundOrderEnumContants.OrderSource;
import com.winit.oms.spi.vo.EbayOrderVo;
import com.winit.oms.spi.vo.v2.order.BuyerInfo;
import com.winit.oms.spi.vo.v2.order.ProductInfo;
import com.winit.oms.spi.vo.v2.order.WarehouseInfo;
import com.winit.openapi.wh.utils.JsonValid;

public class IntelligentRecommendBuilder {

    /**
     * 批量导入订单构建
     * 
     * @param json
     * @return
     */
    public static SourceInfoVo build(JSONObject jsons) {
        JsonValid json = new JsonValid(jsons);

        SourceInfoVo target = new SourceInfoVo();
        // 基础信息
        target.setOrderSource(StringUtils.isEmpty(json.getString("orderSource")) ? OrderSource.MI.getSource() : json.getString("orderSource"));
        target.setPlatform(json.getString("platform"));
        target.setIsRepeat(StringUtils.isEmpty(json.getString("isRepeat")) ? "N" : json.getString("isRepeat"));
        target.setSellerOrderNo(json.getString("sellerOrderNo"));
        target.setSaleType(json.getString("saleType"));
        target.setBulkType(StringUtils.isEmpty(json.getString("bulkType")) ? json.getString("bulkType") : OutboundOrderEnumContants.bulkType.NLM.getStatus());
        target.setInvoiceUrl(json.getString("invoiceUrl"));
        target.setInsuranceType(json.getString("insuranceType"));
        // 产品信息
        ProductInfo productInfo = new ProductInfo();
        productInfo.setWinitProductCategory3(json.getString("winitProductCategory"));
        productInfo.setWinitProductCode(json.getString("winitProductCode"));
        productInfo.setWinitProductName(json.getString("winitProductName"));
        target.setProductInfo(productInfo);
        // 仓库信息
        WarehouseInfo warehouseInfo = new WarehouseInfo();
        warehouseInfo.setWarehouseCode(json.getString("warehouseCode"));
        warehouseInfo.setWarehouseName(json.getString("warehouseName"));
        target.setDispatchWarehouseInfo(warehouseInfo);
        // 买家信息
        BuyerInfo info = new BuyerInfo();
        info.setBuyerName(json.getString("buyerName"));// 收货人姓名
        info.setBuyerEmail(json.getString("buyerEmail"));// 收货人Email
        info.setBuyerPhone(json.getString("buyerPhone"));// 收货人电话
        info.setBuyerHouseNo(json.getString("buyerHouseNo"));// 收货人门牌号
        info.setBuyerCountry(json.getString("buyerCountry"));// 收件人国家名称
        info.setBuyerCountryCode(json.getString("buyerCountryCode"));// 收件人国家编码
        info.setBuyerState(json.getString("buyerState"));// 收货人州名称
        info.setBuyerStateCode(json.getString("buyerStateCode"));// 收货人州编码
        info.setBuyerCity(json.getString("buyerCity"));// 收货人城市名称
        info.setBuyerCityCode(json.getString("buyerCityCode"));// 收货人城市编码
        info.setBuyerPostcode(json.getString("buyerPostcode"));// 收件人邮编
        info.setBuyerAddress1(json.getString("buyerAddress1"));// 收货人街道1
        info.setBuyerAddress2(json.getString("buyerAddress2"));// 收货人街道2
        target.setBuyerInfoVo(info);
        // ebay信息
        EbayOrderVo ebayOrderVo = new EbayOrderVo();
        ebayOrderVo.setIsEbay(json.getString("isEbay"));// 是否eBay订单
        ebayOrderVo.setEbayOrderId(jsons.containsKey("ebayOrderId") ? jsons.getString("ebayOrderId") : null);// eBay订单ID
        ebayOrderVo.setEbayOrderNo(jsons.containsKey("ebayOrderNo") ? jsons.getString("ebayOrderNo") : null);// eBay订单号
        ebayOrderVo.setEbaySellerId(json.getString("ebaySellerId"));
        ebayOrderVo.setEbayBuyerId(json.getString("ebayBuyerId"));
        target.setEbayOrderVo(ebayOrderVo);
        // 商品信息
        List<SourceMerchandiseVo> merchandiseInfoList = new ArrayList<SourceMerchandiseVo>();
        JSONArray array = json.getJSONArray("merchandiseInfoList");
        for (Object item : array) {
            JSONObject obj = (JSONObject) item;
            SourceMerchandiseVo merchandiseVo = new SourceMerchandiseVo();
            merchandiseVo.setQty(obj.getInteger("quantity"));// 商品数量
            merchandiseVo.setMerchandiseCode(obj.getString("merchandiseCode"));// 商品编码
            merchandiseVo.setSpecification(StringUtils.isEmpty(obj.getString("specification")) ? null : obj.getString("specification"));// 规格
            merchandiseVo.setEbayItemId(obj.getString("ebayItemId"));
            merchandiseVo.setEbayTransactionId(obj.getString("ebayTransactionId"));
            merchandiseInfoList.add(merchandiseVo);
        }
        target.setMerchandiseList(merchandiseInfoList);

        return target;
    }

}
